package com.ocom.human.controller;


import com.github.pagehelper.PageInfo;
import com.ocom.common.constant.HumanServiceUrl;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.request.human.StudentsDetailRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.human.service.PersonnelFaceFeatureService;
import com.ocom.human.service.PersonnelInfoService;
import com.ocom.human.utils.FeatureRunnableTask;
import com.ocom.human.utils.FeatureThreadTaskUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class TimerController {


    //http://localhost:9301/test456?comId=100
    //http://localhost:9302/test456?comId=600

    @Autowired
    private PersonnelInfoService personnelInfoService;
    @Autowired
    private PersonnelFaceFeatureService personnelFaceFeatureService;

    //这个是数据库信息加密的
    @GetMapping("/test456")
    public Result enery(@RequestParam(value = "comId" ,defaultValue = "100")Long comId){
        StudentsDetailRequest studentsDetailRequest =new StudentsDetailRequest();
        studentsDetailRequest.setPage(1);
        studentsDetailRequest.setSize(1);
        PageInfo<EntityPersonnelInfo> personnelInfoPage =   personnelInfoService.getKqAndStudentsDetail2(comId,studentsDetailRequest);

        int pages = ((int) personnelInfoPage.getTotal() + 500 -1 )/500;

        List<EntityPersonnelInfo> list =new ArrayList<>();

        for (int i= 1 ;i<=pages;i++ ){

            studentsDetailRequest.setPage(i);
            studentsDetailRequest.setSize(500);

            personnelInfoPage =   personnelInfoService.getKqAndStudentsDetail2(comId,studentsDetailRequest);


            for (EntityPersonnelInfo entityPersonnelInfo: personnelInfoPage.getList()){
                if(entityPersonnelInfo.getComId()>1000l){
                    continue;
                }
                //原手机号 身份证 一致性检查
//                String key = MD5.MD5Encode(entityPersonnelInfo.getComId().toString(), "utf8");
//                if(!CommonUtil.isNull(entityPersonnelInfo.getPPhone())){
//                    if(CommonUtil.isNull(entityPersonnelInfo.getEncryPhone())){
//                        continue;
//                    }
//                    String deide = AES.decrypt(entityPersonnelInfo.getEncryPhone(), key);
//                    if(CommonUtil.isNull(deide)){
//                     //   log.error("异常人员信息: 【{}】【{}】【{}】" , entityPersonnelInfo.getPId(),deide,entityPersonnelInfo.getPPhone() );
//                        continue;
//                    }
//                    deide = deide.replace(" \\t","");
//
//                    if(!deide.equals(entityPersonnelInfo.getPPhone())){
//                        list.add(entityPersonnelInfo);
//                        log.error("异常人员信息: 【{}】【{}】【{}】" , entityPersonnelInfo.getPId(),deide,entityPersonnelInfo.getPPhone() );
//                    }
//                }

                entityPersonnelInfo.setPPwd(entityPersonnelInfo.getPPwd());
//                if(CommonUtil.isNull(entityPersonnelInfo.getPPhone())){
//                    entityPersonnelInfo.setPPhone("");
//                    entityPersonnelInfo.setEncryPhone("");
//                }else{
//                    String phone = entityPersonnelInfo.getPPhone().replace("\\","");
//                    phone = phone.replace("n","");
//                    phone = phone.replace("t","");
//                    entityPersonnelInfo.setEncryPhone(CommonUtil.encryptPhone(phone));
//                    entityPersonnelInfo.setPPhone(phone);
//                }
//                if(CommonUtil.isNull(entityPersonnelInfo.getPIdentity())){
//                    entityPersonnelInfo.setPIdentity("");
//                    entityPersonnelInfo.setEncryIdentity("");
//                }else{
//                    entityPersonnelInfo.setEncryIdentity(CommonUtil.encryptIdCard(entityPersonnelInfo.getPIdentity()));
//                    entityPersonnelInfo.setPIdentity(entityPersonnelInfo.getPIdentity());
//                }
//
//                if(CommonUtil.isNull(entityPersonnelInfo.getBankNo())){
//                    entityPersonnelInfo.setBankNo("");
//                }else{
//                    entityPersonnelInfo.setBankNo(entityPersonnelInfo.getBankNo());
//                }
                try {
                    personnelInfoService.updateById(entityPersonnelInfo.getComId(),entityPersonnelInfo);
                }catch (Exception e){
                    e.printStackTrace();
                    return ResultUtil.info(500,e.getMessage(),entityPersonnelInfo);
                }


            }
        }
        return ResultUtil.success(list);
    }




    @GetMapping(HumanServiceUrl.URL_TimerPersonnelFeature)
    public Result feature() {
        List<EntityPersonnelInfo> personnelInfoList1 = personnelInfoService.getEmptyFeaturePhoto(100l);
        handFeature(personnelInfoList1);
        List<EntityPersonnelInfo> personnelInfoList2 = personnelInfoService.getEmptyFeaturePhoto(600l);
        handFeature(personnelInfoList2);
        return ResultUtil.success();
    }

    public Logger logger = LoggerFactory.getLogger(TimerController.class);

    private void handFeature(List<EntityPersonnelInfo> list){


        for (EntityPersonnelInfo entityPersonnelInfo:list){
            FeatureThreadTaskUtils.run(new FeatureRunnableTask(entityPersonnelInfo,personnelFaceFeatureService,logger));
        //    personnelFaceFeatureService.addEntityPersonnelFaceFeature(entityPersonnelInfo.getComId(),entityPersonnelInfo,logger);
        }
    }


}
